from rest_framework.generics import ListAPIView
from rest_framework.viewsets import ModelViewSet

from goods.models import Goods, Brand, GoodsCategory, GoodsSpecification
from meiduo_admin.serializers.spu import SPUSerializer, SpuSerializer, SPUCategorieSerializer, GoodssSpecsSerializer
from meiduo_admin.utils import PageNum

# 获取SPU表列表数据
class SPUModelViewSet(ModelViewSet):

    serializer_class = SPUSerializer

    pagination_class = PageNum

    def get_queryset(self):
        # 提取keyword
        keyword = self.request.query_params.get('keyword')

        if keyword == '' or keyword is None:
            return Goods.objects.all()
        else:
            return Goods.objects.filter(name=keyword)

# 获取品牌信息
class SPUSimpleView(ListAPIView):
    serializer_class = SpuSerializer
    queryset = Brand.objects.all()

# 获取一级分类信息
class SPUCategoriesView(ListAPIView):

    serializer_class = SPUCategorieSerializer
    queryset = GoodsCategory.objects.filter(id__lt=38)

# 获取二三级分类信息
class SPUCategories2View(ListAPIView):
    serializer_class = SPUCategorieSerializer
    def get_queryset(self):
        pk = self.kwargs['pk']
        return GoodsCategory.objects.filter(parent_id=pk)

# 获取规格表列表数据
class GoodssSpecsView(ModelViewSet):

    serializer_class = GoodssSpecsSerializer

    pagination_class = PageNum

    def get_queryset(self):
        # 提取keyword
        keyword = self.request.query_params.get('keyword')

        if keyword == '' or keyword is None:
            return GoodsSpecification.objects.all()
        else:
            return GoodsSpecification.objects.filter(name=keyword)